package com.loe.mvp.util

import org.greenrobot.eventbus.EventBus
import kotlin.reflect.KClass

/**
 *  发送EventBus消息
 *  @param path 页面路由path
 */
fun postBusMsg(path: String, msg: String)
{
    EventBus.getDefault().post(BusMessage(path, msg))
}

fun postBusMsg(cls: Class<*>, msg: String)
{
    EventBus.getDefault().post(BusMessage(LoeRouter.getPath(cls), msg))
}

fun postBusMsg(kCls: KClass<*>, msg: String)
{
    EventBus.getDefault().post(BusMessage(LoeRouter.getPath(kCls.java), msg))
}

fun postBusMsg(msg: String)
{
    EventBus.getDefault().post(BusMessage("", msg))
}

class BusMessage(var path: String, var msg: String)

const val CLOSE_PAGE = "closePage"

const val UPDATE_PAGE = "updatePage"

/** 关闭页面 */
fun closePage(path: String)
{
    postBusMsg(path, CLOSE_PAGE)
}

fun closePage(cls: Class<*>)
{
    postBusMsg(cls, CLOSE_PAGE)
}

fun closePage(kCls: KClass<*>)
{
    postBusMsg(kCls, CLOSE_PAGE)
}

/** 更新页面 */
fun updatePage(path: String)
{
    postBusMsg(path, UPDATE_PAGE)
}

fun updatePage(cls: Class<*>)
{
    postBusMsg(cls, UPDATE_PAGE)
}

fun updatePage(kCls: KClass<*>)
{
    postBusMsg(kCls, UPDATE_PAGE)
}

///** 打开页面 */
//fun Context.openPage(url: String, delay: Long = 0): Intent?
//{
//    if (url.startsWith("http"))
//    {
//        return LoeRouter.onWebOpen?.invoke(this, url, delay)
//    } else
//    {
//        safe()
//        {
//            val sUrl = SUrl(url)
//            // 未被拦截
//            if (LoeRouter.onPageIntercept?.invoke(this, sUrl) != true)
//            {
//                val intent = start(LoeRouter.getClass(sUrl.path), delay)
//                    .putExtras(sUrl.paramMap)
//                if (sUrl.getBoolean("isClearStack"))
//                {
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
//                }
//                return intent
//            }
//        }
//    }
//    return null
//}